package com.markerhub.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.markerhub.entity.SysFood;
import com.markerhub.service.SysFoodService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jsx on 2023/2/25
 **/
public class FoodListener extends AnalysisEventListener<SysFood> {

    private SysFoodService sysFoodService;
    private List<SysFood> list = new ArrayList<>();

    public FoodListener(SysFoodService sysFoodService){
        this.sysFoodService = sysFoodService;
    }
    @Override
    public void invoke(SysFood sysFood, AnalysisContext analysisContext) {
        sysFood.setId(IdWorker.getIdStr());
        list.add(sysFood);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
         saveData();
    }

    private void saveData(){
        this.sysFoodService.insertAllFood(list);
    }
}
